package com.zh.api.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.utils.FastdfsClientUtil;
import com.zh.common.utils.MultipartFileUtils;
import com.zh.common.utils.ResultVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by caogang on 2019/5/28.
 */

@Controller
@Api("文件上传")
public class FilesUploadController {

    @Autowired
    private FastdfsClientUtil fastdfsClientUtil;

    /**
     * 文件上传
     * */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultVO uploadImg (@RequestParam("file") MultipartFile file ) throws Exception {
        String str= fastdfsClientUtil.upload(file);
        Map params = new HashMap();
        params.put("url", str);
        return ResultVO.success(params);
    }


    @RequestMapping(value = "uploadByBase64", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultVO uploadByBase64 (@RequestBody JSONObject jsonParam) throws Exception {
        String data=jsonParam.getString("data");
        String fileName=jsonParam.getString("fileName");
        MultipartFile multipartFile = MultipartFileUtils.base64toMultipart(data, fileName);
        String str= fastdfsClientUtil.upload(multipartFile);
        Map params = new HashMap();
        params.put("url", str);
        return ResultVO.success(params);
    }

}
